﻿/***************************************************************************
//* Foxit Software Inc.
//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
//* and trade secrets of Foxit Software Inc.                                                      *
***************************************************************************/

/**
* 简单的Rect类
*/

define(function (require, exports, module) {
    var WebPDF = require('../WebPDF.js');
    /**
     * An utility class used to manipulate rectangle objects.
     * @namespace
     * @memberOf WebPDF
     */
    WebPDF.RectUtils = {
    	/**
    	  * @description Clone an new PDFRect object from an exist PDFRect object.
    	  * @param {WebPDF.PDFRect} srcRect The PDFRect object to clone.
    	  * @return {WebPDF.PDFRect} An new PDFPoint object.
    	*/
        clone: function (srcRect) {
            return new WebPDF.PDFRect(srcRect.left, srcRect.top, srcRect.right, srcRect.bottom);
        },
        /**
         * @description Copy value from a PDFRect to another PDFRect.
 		* @param {WebPDF.PDFRect} srcRect The PDFRect object to copy from.
         * @param {WebPDF.PDFRect} destRect The PDFRect object to copy to.
         */
        copy: function (srcRect, destRect) {
            destRect.left = srcRect.left;
            destRect.top = srcRect.top;
            destRect.right = srcRect.right;
            destRect.bottom = srcRect.bottom;
        },
        /**
         * Check whether a PDFRect object is equal to another.
         * @param {WebPDF.PDFRect} srcRect The PDFRect object compare.
         * @param {WebPDF.PDFRect} destRect Another PDFRect object compare.
         * @returns {boolean} Return true if the PDFRect objects are equal.
         */
        equal: function(srcRect,destRect)
        {
        	return (destRect.left === srcRect.left &&
            destRect.top === srcRect.top &&
            destRect.right === srcRect.right &&
            destRect.bottom === srcRect.bottom);
        },
        /**
         * @description Get width of a PDFRect object.
 		 * @param {WebPDF.PDFRect} srcRect The PDFRect object.
         * @return {Number} The width of the PDFRect object.
         */
        width: function (srcRect) {
            return Math.abs(srcRect.right - srcRect.left);
        },
        /**
         * @description Get height of a PDFRect object.
 		 * @param {WebPDF.PDFRect} srcRect  The PDFRect object.
         * @return {Number} Height of the PDFRect object.
         */
        height: function (srcRect) {
            return Math.abs(srcRect.bottom - srcRect.top);
        },
        /**
         * @description Set a PDFRect object to empty.
 		 * @param {WebPDF.PDFRect} srcRect  The PDFRect object to set.
         */
        empty: function (srcRect) {
            srcRect.left = srcRect.top = srcRect.right = srcRect.bottom = 0;
        },
        /**
         * @description Check whether a PDFRect object is empty.
 		 * @param {WebPDF.PDFRect} srcRect  The PDFRect object to check.
         * @return {Boolean} Return true if the PDFRect object is empty, otherwise return false.
         */
        isEmpty: function (srcRect) {
            return srcRect.left >= srcRect.right || srcRect.bottom >= srcRect.top;
        },
        /**
         * @description Offset a PDFRect object.
 		 * @param {WebPDF.PDFRect} srcRect  The PDFRect object to offset.
         * @param {Number} x The x-coordinate to offset.
 		 * @param {Number} y The y-coordinate to offset.
         */
        offset: function (srcRect, x, y) {
            srcRect.left += x;
            srcRect.right += x;
            srcRect.top += y;
            srcRect.bottom += y;
        },
        /**
         * @description Check whether a point is in a specified rectangle.
 		 * @param {WebPDF.PDFRect} srcRect The PDFRect object.
 		 * @example 
 			var isPtInRect = WebPDF.RectUtils.ptInRect(rect, point);
 			var isPtInRect = WebPDF.RectUtils.ptInRect(rect, x, y);
         * @return {Boolean} Return true if the point is in the rectangle, otherwise return false.
         */
        ptInRect: function (srcRect) {
            var x = 0, y = 0;
            if (arguments.length == 2) {
                x = arguments[1].x;
                y = arguments[1].y;
            } else if (arguments.length == 3 && !isNaN(arguments[1]) && !isNaN(arguments[2])) {
                x = arguments[1];
                y = arguments[2];
            }
            return (x >= srcRect.left && x <= srcRect.right && y <= srcRect.top && y >= srcRect.bottom);
        },
        /**
         * @description Intersect two rectangles.
 		 * @param {WebPDF.PDFRect} srcRect  The first rectangle to intersect.
 		 * @param {WebPDF.PDFRect} otherRect The second rectangle to intersect.
         */
        intersect: function (srcRect, otherRect) {
            srcRect.left = Math.max(srcRect.left, otherRect.left);
            srcRect.top = Math.min(srcRect.top, otherRect.top);
            srcRect.right = Math.min(srcRect.right, otherRect.right);
            srcRect.bottom = Math.max(srcRect.bottom, otherRect.bottom);
          //  return (new WebPDF.CPDF_Rect(left, top, right, bottom));
        },
        /**
         * @description Normalize a rectangle to make sure that: rect.left<=rect.right and rect.bottom<=rect.top.
 		 * @param {WebPDF.PDFRect} srcRect The rectangle to normalize.
         */
        normalize: function (srcRect) {
            var tmp;
            if (srcRect.left > srcRect.right) {
                tmp = srcRect.left;
                srcRect.left = srcRect.right;
                srcRect.right = tmp;
            }
            if (srcRect.bottom > srcRect.top) {
                tmp = srcRect.bottom;
                srcRect.bottom = srcRect.top;
                srcRect.top = tmp;
            }
        },
        /**
         * @description Union two rectangles.
 		 * @param {WebPDF.PDFRect} srcRect  The target rectangle to union.
 		 * @param {WebPDF.PDFRect} otherRect The rectangle to union.
         */
        union: function (srcRect, otherRect) {
            srcRect.left = Math.min(srcRect.left, otherRect.left);
            srcRect.top = Math.max(srcRect.top, otherRect.top);
            srcRect.right = Math.max(srcRect.right, otherRect.right);
            srcRect.bottom = Math.min(srcRect.bottom, otherRect.bottom);
        },
        deflate: function(rect, deflateValue){
            rect.left += deflateValue;
            rect.right -= deflateValue;
            rect.top += deflateValue;
            rect.bottom -= deflateValue;
        },
        setRect:function(srcRect)
        {
            if (arguments.length == 2) {
                WebPDF.RectUtils.copy(arguments[1], this);
            } else if (arguments.length == 5) {
                if (!isNaN(arguments[1]) && !isNaN(arguments[2]) && !isNaN(arguments[3]) && !isNaN(arguments[4])) {
                    srcRect.left = arguments[1];
                    srcRect.top = arguments[2];
                    srcRect.right = arguments[3];
                    srcRect.bottom = arguments[4];
                }
            }
        }
    };
    /**
     * Create a new PDFRect with the x and y coordinates of the upper-left and lower right points.
     * @class Represents a utility class used to manipulate rectangle objects.
     * @constructor
     * @example
     *  var rect1 = new WebPDF.PDFRect(left,top,right,bottom);
     *  var rect2 = new WebPDF.PDFRect(anotherRect);
     * @return {WebPDF.PDFRect} An new PDFRect object.
     * @memberOf WebPDF
     */
    WebPDF.PDFRect = function () {
        this.left = this.top = this.right = this.bottom = 0;
        switch (arguments.length) {
            case 1: WebPDF.RectUtils.setRect(this, arguments[0]); break;
            case 4: WebPDF.RectUtils.setRect(this, arguments[0], arguments[1], arguments[2], arguments[3]); break;
        }
    };

    return WebPDF.PDFRect;
});
